#include "JLogger.h"
#include "JVerifyValidate.h"
#include "JTime.h"
#include <fstream>
#include <stdio.h>

std::string JLogger::logFileName;

JLogger::JLogger(void)
{
}


JLogger::~JLogger(void)
{
}

void JLogger::Init( const char* _logFileDir )
{
	std::string fileName("JSubVersionLogFile ");
	fileName += JTime::GetFileNameTime() + ".log";
	if(JVerifyValidate::IsValidateFileName(_logFileDir))
	{
		logFileName = std::string(_logFileDir) + "\\" + fileName;
	}
	else
	{
		logFileName = std::string(".\\") + fileName;
	}
}

void JLogger::Log( const char* msg,JLogLevel level )
{
	/*std::string tmp("Log level:");
	tmp += LevelToStr(level);
	tmp += ""*/
	char buff[200];
	sprintf(buff,"%s Log level:%s, Message:%s\n",JTime::GetNormalTime().c_str(),JLogger::LevelToStr(level).c_str(),msg);
	WriteStrToFile(buff);
}

void JLogger::Log( const std::string msg,JLogLevel level )
{
	Log(msg.c_str(),level);
}

void JLogger::WriteStrToFile( const char* msg )
{

	std::ofstream out(logFileName.c_str(),std::ios::in | std::ios::app);
	if(!out)
	{
		/////////throw exception.
	}
	else
	{
		out << msg;
		out.close();
	}
}

std::string JLogger::LevelToStr( JLogLevel level )
{
	switch(level)
	{
	case JLogMessage:
		return "Message";
	case JLogWarning:
		return "Warning";
	case JLogError:
		return "Error";
	default:
		return "";
	}
}
